package com.gjy.design.proxy.jdkp;

import com.gjy.design.proxy.OrderService;
import com.gjy.design.proxy.OrderServiceImpl;

import java.lang.reflect.Proxy;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-11-10 10:45:59
 */
public class Demo {

    @SuppressWarnings("unchecked")
    public static <T> T createProxy(T target, Class<?>[] interfaces) {
        Class<?> cls = target.getClass();

        return (T) Proxy.newProxyInstance(
                cls.getClassLoader(),
                // interfaces,
                cls.getInterfaces(),
                new LoggingInvocationHandler(target)
        );
    }

    public static void main(String[] args) {
        OrderServiceImpl real = new OrderServiceImpl();
        OrderService proxy = createProxy(real, new Class[]{OrderService.class});
        proxy.createOrder("A-200", 5);
        proxy.cancelOrder("ORD-002");
    }

}
